﻿package 
{
    import Buff.*;
    import com.assist.*;
    import com.assist.view.interfaces.*;
    import com.haloer.display.*;
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;

    public class Buff extends Sprite
    {
        private var _onDoubleRemoveBuff:Function;
        private var _tip:ITip;
        private var _allBuffData:Object;
        private var _allBuffList:Array;
        private var _buffMc:MovieClip;
        private var _isShow:Boolean = true;

        public function Buff()
        {
            this._onDoubleRemoveBuff = new Function();
            this._allBuffData = {};
            this._allBuffList = [];
            return;
        }// end function

        public function set addBuff(param1:Object) : void
        {
            this.contrastBuff(param1);
            return;
        }// end function

        public function set removeBuff(param1:String) : void
        {
            this.clearBuff(param1);
            return;
        }// end function

        public function set onDoubleRemoveBuff(param1:Function) : void
        {
            this._onDoubleRemoveBuff = param1;
            return;
        }// end function

        public function set tip(param1:ITip) : void
        {
            this._tip = param1;
            return;
        }// end function

        private function contrastBuff(param1:Object) : void
        {
            var _loc_2:* = this._allBuffList.length;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2)
            {
                
                if (param1.type == this._allBuffList[_loc_3])
                {
                    this.buffDataChange(param1, true);
                    return;
                }
                _loc_3 = _loc_3 + 1;
            }
            this.addBuffRender(param1);
            return;
        }// end function

        private function addBuffRender(param1:Object) : void
        {
            this.addMc();
            param1.mc = this._buffMc;
            param1.doubleClick = this.doubleClick(param1.type);
            this._buffMc.addEventListener(MouseEvent.DOUBLE_CLICK, param1.doubleClick);
            var _loc_2:* = new TextField();
            param1.textField = _loc_2;
            var _loc_3:* = new TextFormat();
            param1.textFormat = _loc_3;
            this._allBuffData[param1.type] = param1;
            this._allBuffList.push(param1.type);
            this.buffDataChange(param1, false);
            return;
        }// end function

        private function addMc() : void
        {
            this._buffMc = new MovieClip();
            addChild(this._buffMc);
            this._buffMc.buttonMode = true;
            this._buffMc.visible = this._isShow;
            return;
        }// end function

        private function buffDataChange(param1:Object, param2:Boolean) : void
        {
            var _loc_4:String = null;
            var _loc_3:* = this._allBuffData[param1.type];
            _loc_3.name = param1.name;
            _loc_3.effect = param1.effect;
            _loc_3.info = param1.info;
            _loc_3.mc.doubleClickEnabled = param1.doubleClickable;
            if (param1.sign == _loc_3.sign)
            {
            }
            if (param2 == false)
            {
                _loc_4 = URI.buffIconUrl + param1.sign + ".png";
                _loc_3.url = _loc_4;
                _loc_3.sign = param1.sign;
                this.loadBuffIcon(_loc_3);
            }
            _loc_3.textField.htmlText = this.renderBuffInfo(_loc_3);
            _loc_3.textField.autoSize = TextFieldAutoSize.LEFT;
            _loc_3.textFormat.leading = 8;
            _loc_3.textField.setTextFormat(_loc_3.textFormat);
            if (param2 == false)
            {
                this._tip.addTarget(_loc_3.mc, _loc_3.textField);
            }
            this.sortBuffList();
            return;
        }// end function

        private function doubleClick(param1:String) : Function
        {
            var type:* = param1;
            var func:* = function (event:MouseEvent) : void
            {
                clearBuff(type);
                _onDoubleRemoveBuff(type);
                return;
            }// end function
            ;
            return func;
        }// end function

        private function loadBuffIcon(param1:Object) : void
        {
            var icon:Image;
            var data:* = param1;
            this.clearIcon(data.type);
            icon = new Image(data.url);
            data.icon = icon;
            icon.onComplete = function () : void
            {
                icon.width = 24;
                icon.height = 24;
                data.mc.addChild(icon);
                return;
            }// end function
            ;
            icon.mouseEnabled = false;
            return;
        }// end function

        private function clearIcon(param1:String) : void
        {
            var _loc_4:Object = null;
            var _loc_2:* = this._allBuffList.length;
            var _loc_3:* = _loc_2 - 1;
            while (_loc_3 >= 0)
            {
                
                if (param1 == this._allBuffList[_loc_3])
                {
                    _loc_4 = this._allBuffData[param1];
                    if (_loc_4.icon != null)
                    {
                    }
                    if (_loc_4.icon.parent)
                    {
                        _loc_4.icon.parent.removeChild(_loc_4.icon);
                        _loc_4.icon = null;
                        return;
                    }
                }
                _loc_3 = _loc_3 - 1;
            }
            return;
        }// end function

        private function renderBuffInfo(param1:Object) : String
        {
            var _loc_2:String = "";
            if (param1.name != "")
            {
                _loc_2 = htmlFormat(param1.name, 14, 16777215, false) + "\n";
            }
            _loc_2 = _loc_2 + (htmlFormat(param1.effect, 14, 65331, false) + htmlFormat("\n" + param1.info, 12, 16777215, false));
            return _loc_2;
        }// end function

        private function sortBuffList() : void
        {
            var _loc_3:Object = null;
            var _loc_1:* = this._allBuffList.length;
            var _loc_2:int = 0;
            while (_loc_2 < _loc_1)
            {
                
                _loc_3 = this._allBuffData[this._allBuffList[_loc_2]];
                _loc_3.mc.x = 91 + 30 * _loc_2;
                _loc_3.mc.y = 97;
                _loc_2 = _loc_2 + 1;
            }
            return;
        }// end function

        private function clearBuff(param1:String) : void
        {
            var _loc_4:Object = null;
            var _loc_2:* = this._allBuffList.length;
            var _loc_3:* = _loc_2 - 1;
            while (_loc_3 >= 0)
            {
                
                if (param1 == this._allBuffList[_loc_3])
                {
                    _loc_4 = this._allBuffData[param1];
                    if (_loc_4.icon != null)
                    {
                    }
                    if (_loc_4.icon.parent)
                    {
                        _loc_4.icon.parent.removeChild(_loc_4.icon);
                        _loc_4.icon = null;
                    }
                    _loc_4.mc.removeEventListener(MouseEvent.DOUBLE_CLICK, _loc_4.doubleClick);
                    removeChild(_loc_4.mc);
                    delete this._allBuffData[param1];
                    this._allBuffList.splice(_loc_3, 1);
                    this.sortBuffList();
                }
                _loc_3 = _loc_3 - 1;
            }
            return;
        }// end function

        public function clear() : void
        {
            var _loc_3:String = null;
            var _loc_1:* = this._allBuffList.length;
            var _loc_2:* = _loc_1 - 1;
            while (_loc_2 >= 0)
            {
                
                _loc_3 = this._allBuffList[_loc_2];
                this.clearBuff(_loc_3);
                _loc_2 = _loc_2 - 1;
            }
            return;
        }// end function

        public function set visibleBuff(param1:Boolean) : void
        {
            var _loc_4:String = null;
            var _loc_5:Object = null;
            var _loc_2:* = this._allBuffList.length;
            var _loc_3:* = _loc_2 - 1;
            while (_loc_3 >= 0)
            {
                
                _loc_4 = this._allBuffList[_loc_3];
                _loc_5 = this._allBuffData[_loc_4];
                _loc_5.mc.visible = param1;
                _loc_3 = _loc_3 - 1;
            }
            this._isShow = param1;
            return;
        }// end function

    }
}
